<?php

class DanhMucBanTinController extends Zend_Controller_Action {
	
	public function init() {
		/* Initialize action controller here */
	}
	
	public function indexAction() {
		// action body
	}
	
	private function getTaoDanhMucForm() {
		$form = new Zend_Form ();
		$form->setAction ( 'new-success' );
		$form->setMethod ( 'post' );
		$form->setDescription ( "Tạo danh mục" );
		
		$form->addElement ( 'text', 'tenDanhMuc' );
		$input_tenDM = $form->getElement ( 'tenDanhMuc' );
		$input_tenDM->setLabel ( 'Tên danh mục :' );
		$input_tenDM->setOrder ( 1 )->setRequired ( true );
		
		$form->addElement ( 'textarea', 'chuThich' );
		$txt_chuThich = $form->getElement ( 'chuThich' );
		$txt_chuThich->setLabel ( 'Chú thích:' );
		$txt_chuThich->setOrder ( 2 );
		
		$form->addElement ( 'submit', 'submit' );
		$submitButton = $form->getElement ( 'submit' );
		$submitButton->setLabel ( 'Tạo danh mục!' );
		$submitButton->setOrder ( 4 );
		
		//add vilidator
		$input_tenDM->addValidator ( new Zend_Validate_StringLength ( 5, 30 ) );
		//add fitter
		$input_tenDM->addFilter ( new Zend_Filter_StringToLower () );
		$input_tenDM->addFilter ( new Zend_Filter_StripTags () );
		$txt_chuThich->addFilter ( new Zend_Filter_StripTags () );
		return $form;
	
	}
	
	public function newAction() {
		//Add the form to the view
		$form = $this->getTaoDanhMucForm ();
		$this->view->form = $form;
	}
	
	public function newSuccessAction() {
		$form = $this->getTaoDanhMucForm ();
		//check if form post
		if ($form->isValid ( $_POST )) {
			$tenDanhMuc = $form->getValue ( 'tenDanhMuc' );
			$chuThich = $form->getValue ( 'chuThich' );
			
			$danhMucBanTinService = Application_Model_service_DanhMucBanTinService::getInstance ();
			$result = $danhMucBanTinService->taoDanhMuc ( $tenDanhMuc, $chuThich );
			
			$this->view->result = $result;
		} else {
			$this->view->error = $form->getMessages ();
			$this->view->form = $form;
		}
	}
	
	public function editAction() {
		// action body
		$id = $_POST ['updateDanhMuc'];
		$danhMucBT = Application_Model_service_DanhMucBanTinService::getInstance ();
		$danhMuc = $danhMucBT->getMotDanhMucBanTin ( $id );
		//addView
		$this->view->danhMuc = $danhMuc;
	}
	
	public function editSuccessAction() {
		// action body
		$id = $this->_request->getParam ( 'maDanhMuc' );
		$ten = $this->_request->getParam ( 'tenDanhMuc' );
		$chuThich = $this->_request->getParam ( 'chuThich' );
		
		//lay service
		$service = Application_Model_service_DanhMucBanTinService::getInstance ();
		$result = $service->suaDanhMuc ( $id, $ten, $chuThich );
		
		//add view
		$this->view->result = $result;
	}
	
	public function deleteAction() {
		// action body
$id = $_POST ['deleteDanhMuc'];
		$DanhMucBT = Application_Model_service_DanhMucBanTinService::getInstance ();
		$result = $DanhMucBT->xoaDanhMuc ( $id );
		
		//addview
		$this->view->result = $result;
	}
	
	public function loadAction() {
		// action body
		$service = Application_Model_service_DanhMucBanTinService::getInstance ();
		$result = $service->getAllDanhMucBanTin ();
		
		//add view
		$this->view->list = $result;
	}
}



